<?php
$productIdField = 'media_productId_orderConfig_info';
$categoryIdField = 'media_categoryId_orderConfig_info';
$productPriceField = 'media_productPrice_orderConfig_info';
$changePriceTypeField = 'media_changePriceType_orderConfig_info';
$changePriceField = 'media_changePrice_orderConfig_info';
$changePriceUnitField = 'media_changePriceUnit_orderConfig_info';
$model = new BDynamicFormModel();
$model->addField($productIdField);
$model->addField($categoryIdField);
$model->addField($productPriceField);
$model->addField($changePriceTypeField);
$model->addField($changePriceField);
$model->addField($changePriceUnitField);

$productConfigTable = new BPSheet('productConfig');
if ($productConfigId > 0) {
    $productConfig = $productConfigTable->getListById($productConfigId);
    $productId = $productConfig->getItem('productId')->getValue();
    $categoryId = $productConfig->getItem('categoryId')->getValue();
    $productPrice = $productConfig->getItem('productPrice')->getValue();
    $changePriceType = $productConfig->getItem('changePriceType')->getValue();
    $changePrice = $productConfig->getItem('changePrice')->getValue();
    $changePriceUnit = $productConfig->getItem('changePriceUnit')->getValue();

    $model->$productIdField = $productId;
    $model->$categoryIdField = $categoryId;
    $model->$productPriceField = $productPrice;
    $model->$changePriceTypeField = $changePriceType;
    $model->$changePriceField = $changePrice;
    $model->$changePriceUnitField = $changePriceUnit;
}
?>
<?php
$app = Blyn::app()->getActiveApp();
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
?>
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "addOrderProductForm",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
$url = $control->getAjaxLinkUrl('saveOrderProductConfig', NULL, array(
    'configId' => $configId,
    'productConfigId' => $productConfigId
        ));
$updateId = $control->getAjaxContainerId();
?>

<link rel="stylesheet" type="text/css"
      href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-datetimepicker.min.css" />
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-datetimepicker.min.js"/>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function saveProductConfig() {

        var formData = new FormData($("#addOrderProductForm")[0]);
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            //datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(html) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(html);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function(data) {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>
<legend>
    <h4>
        <?php echo $screen->getAjaxLink(array('name' => '活动列表', 'id' => 'showEventList')); ?>\
        <?php
        echo $screen->getAjaxLink(array('name' => '配置列表', 'id' => 'showOrderConfigList'), array('eventId' => $eventId));
        ?>\
        <?php
        //echo '产品配置列表'
        echo $control->getAjaxLink(array('name' => '产品配置列表', 'id' => 'showProductConfigList'), array(
            'eventId' => $eventId,
            'configId' => $configId
        ));
        ?>\
        <?php echo '添加产品配置'; ?>
    </h4>
</legend>

<table class="table table-hover">
    <colgroup>
        <col class="span1">
        <col class="span7">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo '属性'; ?></th>
            <th><?php echo '内容'; ?></th>
        </tr>
    </thead>
    <tbody>
        <tr><td> <?php echo '产品ID'; ?> </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $productIdField, array('placeholder' => '名称' . '...', 'required' => true));
                ?>
            </td>
        </tr>               
        <tr>
            <td> <?php echo '产品分类ID'; ?>  </td>                    
            <td>
                <?php
                echo CHtml::activeTextArea($model, $categoryIdField, array('placeholder' => '产品分类' . '...'));
                ?>
        </tr>
        <tr>
            <td>
                <?php echo '产品价格'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $productPriceField, array('placeholder' => '产品价格' . '...', 'required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
        </tr>
        <tr>
            <td>
                <?php echo '调整价格方式'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeRadioButtonList($model, $changePriceTypeField, array('addPrice' => '加价',
                    'discount' => '降价'), array(
                    'labelOptions' => array('style' => 'display:inline'), // add this code
                    'separator' => '  ',
                ));
                //echo $form->activeRadio($model, $changePriceTypeField, array('placeholder' => '调整价格方式' . '...', 'required' => true));
                ?>
            </td>
        </tr>
        <tr>
            <td>
                <?php echo '价格调整量'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $changePriceField, array('required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
        </tr>
        <tr>
            <td>
                <?php echo '改变单位'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeDropDownList($model, $changePriceUnitField, $app->getPriceChangeUnitList(), array('required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
        </tr>
    </tbody>
</table>
<?php
$this->endWidget();
//echo CHtml::endForm();
?>
<button class="btn" onclick="javascript:saveProductConfig()"><?php echo '保存产品设置'; ?></button>
